var app = getApp();

function organize(title, content, images, type, owner, address, begintime, endtime, repeattype, joinable, participants, repeatend, joinsheet, calendar_id) {
  console.log('activity organize start...');
  wx.showLoading();
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.activity.organize',
      title: title,
      content: content,
      images: images,
      type: type,
      owner: owner,
      address: address,
      begintime: begintime,
      endtime: endtime,
      repeattype: repeattype,
      joinable: joinable,
      participants: participants,
      repeatend: repeatend,
      joinsheet: joinsheet,
      my_joinsheet: [],
      joined: false,
      calendar_id: calendar_id, 
      yuyue_session: wx.getStorageSync("yuyue_session"),
    },
    success: function (res) {
      console.log(res);
      var op = res.data.op;
      if (op == "fail") {
        wx.showModal({
          title: '活动发起失败！请重试',
          showCancel: false
        });
        return false;
      }else {
        var data = res.data.data;
        var aid = data.id;
        wx.navigateBack({
          
        });
        return;
        wx.navigateTo({
          url: '../activity/detail?id=' + aid,
        })
      }
    }, 
    complete: function () {
      wx.hideLoading();
    }
  });
}

function organized_all_my_list(callback) {
  console.log('now get my organized_list start...  owner :' + wx.getStorageSync('yuyue_session'));
  wx.showLoading();
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.activity.organized_all_my_list',
      //type: type,
      owner: wx.getStorageSync('yuyue_session')
    },
    success: function (res) {
      callback(res);
    },
    complete: function () {
      wx.hideLoading();
    }
  });
}

function organized_list(type, owner) {
  wx.showLoading();
  console.log('now get my organized_list start... type :' + type + ", owner :" + owner);
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.activity.organized_list',
      type: type,
      owner: owner
    },
    success: function (res) {
      console.log(res);
      var activity = res;
      wx.setStorageSync("personal_organized_list", res)
      return activity;
    },
    complete: function () {
      wx.hideLoading();
    }
  });
} 

function sign(aid, yuyue_session, join_sheet, callback) {
  console.log('now sign start... aid :' + aid + ", yuyue_session :" + yuyue_session);
  console.log(join_sheet);
  wx.showLoading();
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.activity.sign',
      activity_id: aid,
      yuyue_session: yuyue_session,
      sheet: join_sheet,
    },
    success: function (res) {
      console.log(res);
      callback(res);
    }, 
    complete: function () {
      wx.hideLoading();
    }
  });
}

function unsign(aid, yuyue_session, callback) {
  console.log('now unsign start... aid :' + aid + ", yuyue_session :" + yuyue_session); 
  wx.showLoading();
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.activity.unsign',
      activity_id: aid,
      yuyue_session: yuyue_session,
    },
    success: function (res) {
      console.log(res);
      callback(res);
    }, 
    complete: function () {
      wx.hideLoading();
    }
  });
}

function view(activity, callback){
  wx.showLoading();
  console.log('now activity view start... activity :' + activity);
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.activity.view',
      activity: activity, 
      yuyue_session: wx.getStorageSync("yuyue_session"),
    },
    success: function (res) {
      callback(res);
    }, 
    complete: function () {
      wx.hideLoading();
    }
  });
}
function view_join_list(activity_id, yuyue_session, callback) {
  wx.showLoading();
  console.log('now activity view_join_list start... activity_id :' + activity_id);
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.activity.view_join_list',
      activity_id: activity_id,
      yuyue_session: wx.getStorageSync("yuyue_session"),
    },
    success: function (res) {
      callback(res);
    }, 
    complete: function () {
      wx.hideLoading();
    }
  });
} 

function upload_image(img_src, callback) {
  wx.showLoading();
  console.log('now activity upload_image start...');
  wx.uploadFile({
    url: app.globalData.default_url + "?action=api.v1.activity.upload_image",
    filePath: img_src,
    name: 'file',
    success: function (res) {
      callback(res);
    },
    complete: function () {
      wx.hideLoading();
    }
  });
}

function edit(aid, title, content, address, upload_pic_name_list, batch, callback){
  wx.showLoading();
  console.log('now activity modify start... aid :' + aid);
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.activity.edit',
      activity_id: aid,
      title: title,
      content: content, 
      address: address,
      images: upload_pic_name_list,
      yuyue_session: wx.getStorageSync("yuyue_session"),
      batch: batch
    },
    success: function (res) {
      callback(res);
    },
    complete: function () {
      wx.hideLoading();
    }
  });
}

function cancel(aid, batch, callback) {
  wx.showLoading();
  console.log('now activity cancel start... aid :' + aid);
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.activity.cancel',
      activity_id: aid,
      yuyue_session: wx.getStorageSync("yuyue_session"),
      batch: batch
    },
    success: function (res) {
      callback(res);
    }, 
    complete: function () {
      wx.hideLoading();
    }
  });
} 

function start(aid, batch, callback) {
  wx.showLoading();
  console.log('now activity start start... aid :' + aid);
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.activity.start',
      activity_id: aid,
      yuyue_session: wx.getStorageSync("yuyue_session"),
      batch: batch
    },
    success: function (res) {
      callback(res);
    },
    complete: function () {
      wx.hideLoading();
    }
  });
} 

function subscribe(aid, callback) {
  wx.showLoading();
  console.log('now activity subscribe... aid :' + aid);
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.activity.subscribe',
      activity_id: aid,
      yuyue_session: wx.getStorageSync("yuyue_session"),
    },
    success: function (res) {
      callback(res);
    },
    complete: function () {
      wx.hideLoading();
    }
  });
} 

function unsubscribe(aid, callback) {
  wx.showLoading();
  console.log('now activity unsubscribe... aid :' + aid);
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.activity.unsubscribe',
      activity_id: aid,
      yuyue_session: wx.getStorageSync("yuyue_session"),
    },
    success: function (res) {
      callback(res);
    },
    complete: function () {
      wx.hideLoading();
    }
  });
}

function check_subscribe(aid, callback) {
  wx.showLoading();
  console.log('now activity check_subscribe... aid :' + aid);
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.activity.check_subscribe',
      activity: aid,
      yuyue_session: wx.getStorageSync("yuyue_session"),
    },
    success: function (res) {
      callback(res);
    },
    complete: function () {
      wx.hideLoading();
    }
  });
}

module.exports = {
  organize: organize,
  organized_list: organized_list,
  organized_all_my_list: organized_all_my_list,
  view: view,
  sign: sign,
  unsign: unsign,
  view_join_list: view_join_list,
  upload_image: upload_image,
  edit: edit,
  cancel: cancel,
  start: start,
  subscribe: subscribe,
  unsubscribe: unsubscribe,
  check_subscribe: check_subscribe,
}